using MyWebPagesStarterKit.Sitemaps.Utilities;
using System;
using System.IO;
using System.Web;
using System.Xml;

namespace MyWebPagesStarterKit.Sitemaps
{
    public sealed class SitemapHandler : XmlResponseHandlerBase
    {
        protected override void WriteResult(XmlTextWriter result)
        {
            LastModifiedResolveEventHandler handler = null;
            Formatting formatting = Sitemaps.Formatting;
            bool useFileModifiedDate = Sitemaps.UseFileModifiedDate;
            if (!string.IsNullOrEmpty(base.Request.QueryString["formatting"]))
            {
                try
                {
                    formatting = EnumUtils.Parse<Formatting>(base.Request.QueryString["formatting"], true);
                }
                catch (Exception exception)
                {
                    throw new ArgumentException("Error parsing 'formatting'. Valid values are Indented or None.", "formatting", exception);
                }
            }
            if (!string.IsNullOrEmpty(base.Request.QueryString["useFileModifiedDate"]))
            {
                try
                {
                    useFileModifiedDate = Convert.ToBoolean(base.Request.QueryString["useFileModifiedDate"]);
                }
                catch (Exception exception2)
                {
                    throw new ArgumentException("Error parsing 'useFileModifiedDate'. Valid values are True or False.", "useFileModifiedDate", exception2);
                }
            }
            result.Formatting = formatting;
            string siteHostUrl = base.Request.Url.Scheme + "://" + base.Request.Url.Host;
            int port = base.Request.Url.Port;
            if ((port != 80) && (port != 0x1bb))
            {
                siteHostUrl = siteHostUrl + ":" + port;
            }
            SitemapWriter writer = new SitemapWriter(result, siteHostUrl);
            if (useFileModifiedDate)
            {
                if (handler == null)
                {
                    handler = delegate (object sender, LastModifiedResolveEventArgs e) {
                        SiteMapNode siteMapNode = e.SiteMapNode;
                        if (string.IsNullOrEmpty(siteMapNode["lastmod"]))
                        {
                            string path = base.Request.MapPath(siteMapNode.Url);
                            if (File.Exists(path))
                            {
                                return new DateTime?(File.GetLastWriteTimeUtc(path));
                            }
                            if (string.IsNullOrEmpty(Path.GetFileName(path)) && File.Exists(path + "default.aspx"))
                            {
                                return new DateTime?(File.GetLastWriteTimeUtc(path + "default.aspx"));
                            }
                        }
                        return null;
                    };
                }
                writer.LastModifiedResolve += handler;
            }
            writer.WriteSiteMap(SiteMap.RootNode);
        }
    }
}
